package com.yn.module.material.utils.xls;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @description: excel操作工具类
 * @author: 叶甯
 * @create: 2022/10/13 16:24
 * @since: 1.0.0
 * @copyright (C), 2022, https://gitee.com/johnny .All rights reserved.
 */
public class ExcelUtils {
    // 导入的模板名称
    public static final String IMPORT_TPL_PATH = "/tpl/";

    /**
     * 模板下载
     *
     * @param resp
     * @param path 路径
     * @param name 文件名称
     * @throws IOException
     */
    public static void doExportZipTemplate(HttpServletRequest req, HttpServletResponse resp, String path,
                                           String name) throws IOException {
//        String userAgent = req.getHeader("User-Agent");
//        String formatName;
//        // IE 内核
//        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
//            formatName = URLEncoder.encode(name, "UTF-8");
//        }
//        else {
//            // 非IE浏览器
//            formatName = new String(name.getBytes("UTF-8"), "ISO-8859-1");
//        }
        resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        resp.setHeader("content-disposition", String.format("attachment;filename=%s", URLEncoder.encode(name, "UTF-8")));
        resp.setCharacterEncoding("UTF-8");
        ServletOutputStream outputStream = resp.getOutputStream();
        InputStream inStream = ExcelUtils.class.getResourceAsStream(path + name);
        byte[] buffer = new byte[8192];
        int len;
        while ((len = inStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inStream.close();
    }

    public static void downLoadExcel(HttpServletResponse resp, String fileName, Workbook workBook) throws IOException {
        //String encode = new String((fileName).getBytes("UTF-8"), "ISO-8859-1");
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        //response.setHeader("content-disposition","attachment;filename="+encode);
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream outputStream = resp.getOutputStream();
        workBook.write(outputStream);
        workBook.close();
        outputStream.close();

    }


    /**
     * 判断有效标识是否满足校验条件，如果不能返回有效标识，那么返回true，校验失败，提示错误
     *
     * @param ValidCode 导入的Excel中的校验标识
     * @return StdDrugService#checkExcel(int, com.viewhigh.data.mc.web.cost.dto.excel.ExcelInfo, List)
     */
    public static boolean checkValidCode(String ValidCode) {
        return ObjectUtils.isEmpty(ValidCode) || !"Y/N".contains(ValidCode.trim());
    }

}
